package com.haibei.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.common.uitl.DateTool;
import com.common.uitl.ListUtiles;
import com.common.uitl.StringUtils;
import com.haibei.R;
import com.haibei.adapter.bean.Comment;
import com.haibei.adapter.bean.User;
import com.haibei.cache.list.fanou.SimpleImageLoader;

public class CommentsAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<Comment> mArrayListData;

	public CommentsAdapter(Context context, ArrayList<Comment> datas) {
		this.context = context;
		this.mArrayListData = datas;
	}

	@Override
	public int getCount() {
		return mArrayListData.size();
	}

	@Override
	public Object getItem(int position) {
		return mArrayListData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder vl = null;
		ViewSpliter vsl = null;
		int type = getItemViewType(position);
		if (null == convertView) {
			switch (type) {
			case 0: {
				vl = new ViewHolder();
				convertView = View.inflate(context, R.layout.item_article_comments_with_sub, null);
				vl.imgHeader = (ImageView) convertView.findViewById(R.id.asImageHeader);
				vl.textViewAuthor = (TextView) convertView.findViewById(R.id.textViewAuthor);
				vl.textTime = (TextView) convertView.findViewById(R.id.textTime);
				vl.textViewLocation = (TextView) convertView.findViewById(R.id.textViewLocation);
				vl.textViewGood = (TextView) convertView.findViewById(R.id.textViewGood);
				vl.textViewComment = (TextView) convertView.findViewById(R.id.textViewComment);
				vl.imageViewSplit = (ImageView) convertView.findViewById(R.id.imageViewSplit);
				vl.linearLayoutContent = (LinearLayout) convertView.findViewById(R.id.linearLayoutItemContent);
				convertView.setTag(vl);
			}
				break;
			case 1: {
				vsl = new ViewSpliter();
				convertView = View.inflate(context, R.layout.item_comments_divider_split, null);
				vsl.textViewIndicator = (TextView) convertView.findViewById(R.id.textViewIndicator);
				convertView.setTag(vsl);
			}
				break;
			}

		} else {
			switch (type) {
			case 0:
				vl = (ViewHolder) convertView.getTag();
				break;
			case 1:
				vsl = (ViewSpliter) convertView.getTag();
				break;
			}

		}
		Comment p = mArrayListData.get(position);
		switch (type) {
		case 0: {
			if (p.getParentId().equals("0")) {
				// 一级评论
				LinearLayout.LayoutParams lyp1 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
				vl.linearLayoutContent.setLayoutParams(lyp1);
				vl.linearLayoutContent.setBackgroundDrawable(null);
				if (ListUtiles.isEmpty(p.getSubComment())) {
					vl.imageViewSplit.setVisibility(View.VISIBLE);
				} else {
					vl.imageViewSplit.setVisibility(View.INVISIBLE);
				}
			} else {
				// 二级评论
				LinearLayout.LayoutParams lyp2 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
				int margin = (int) context.getResources().getDimension(R.dimen.margin_content);
				lyp2.setMargins(margin, 0, 0, 0);
				vl.linearLayoutContent.setLayoutParams(lyp2);
				vl.linearLayoutContent.setBackgroundResource(R.drawable.bg_comment_sub);
				if ((position + 1) <= (mArrayListData.size() - 1)) {
					Comment tem = mArrayListData.get((position + 1));
					if ("0".equals(tem.getParentId())|| StringUtils.isEmpty(tem.getContent())) {
						// 下一个是一级评论说明二级评论显示完毕
						vl.imageViewSplit.setVisibility(View.VISIBLE);
					} else {
						vl.imageViewSplit.setVisibility(View.INVISIBLE);
					}
				} else {
					vl.imageViewSplit.setVisibility(View.VISIBLE);
				}
			}

			User u = p.getUser();
			if (null != u && !StringUtils.isEmpty(u.getHeadPortrait())) {
				SimpleImageLoader.display(vl.imgHeader, u.getHeadPortrait());
				// ImageManagerLru.from(context).displayImage(vl.imgHeader,
				// u.getHeadPortrait(), R.drawable.bg_default_user_header);
			}
			try{
			vl.textTime.setText(DateTool.geLongTimeMonthDay(Long.parseLong(p.getTime())));
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			vl.textViewGood.setText("赞" + p.getPraise());
			vl.textViewComment.setText(p.getContent());
		}
			break;
		case 1: {
			vsl.textViewIndicator.setText(p.isHot() ? "热门评论" : "最新评论");
		}
			break;
		}

		return convertView;
	}

	@Override
	public int getViewTypeCount() {
		return 3;
	}

	@Override
	public int getItemViewType(int position) {
		Comment com = mArrayListData.get(position);
		if (StringUtils.isEmpty(com.getContent())) {
			return 1;// 分割
		} else {
			return 0;// 真是条目
		}
	}

	class ViewHolder {
		ImageView imgHeader;
		TextView textViewAuthor;
		TextView textTime;
		TextView textViewLocation;
		TextView textViewGood;
		TextView textViewComment;
		ImageView imageViewSplit;
		LinearLayout linearLayoutContent;
	}

	class ViewSpliter {
		TextView textViewIndicator;
	}

}
